package com.zkingsoft.actions.admin.distribution;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.authority.MatrixAdapter;
import com.zkingsoft.constraint.BaseController;
import com.zkingsoft.model.distribution.DistributionImages;
import com.zkingsoft.model.distribution.DistributionProduct;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.distribution.DistributionImagesService;
import com.zkingsoft.services.distribution.DistributionProductService;
import com.zkingsoft.util.WebUtil;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2017-06-16 15:22
 */
@Controller
@RequestMapping(value = "admin/distributionImages")
public class DistributionImagesController extends BaseController{

	@Resource
	private DistributionImagesService distributionImagesService;

	@Resource
	private DistributionProductService distributionProductService;
	@Resource
	private MatrixAdapter matrixAdapter;
	@Value("${file_storage_path}")
	private String fileStoragePath;
	@Value("${nginx_url}")
	private String nginxUrl;
	
	
	//记录编辑前的值Before_Edit_Value
	public static final String BEV="DistributionImages_BEV";
	
	public static final String fnCode = "DistributionProduct";
	public static final String search = fnCode + ":search";
	public static final String edit = fnCode + ":edit";
	public static final String del = fnCode + ":del";
	public static final String add = fnCode + ":add";
	
	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(DistributionImages distributionImages, PaginationVO pageVo) {
		matrixAdapter.isBtnPermitted(search);
		return showList(distributionImagesService, distributionImages, pageVo);
	}
   	
	/**
	 * 新增或者修改页面
	 */   
   	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(DistributionImages distributionImages) {
		if (distributionImages.getImgId() != null) {
	   		matrixAdapter.isBtnPermitted(edit);
	   		distributionImages.setImgId(((DistributionImages)WebUtil.getSessionAttribute(BEV)).getImgId());
	   		AjaxResult result=modify(distributionImagesService, WebUtil.getSessionAttribute(BEV), distributionImages, "图集信息");
			WebUtil.removeSessionAttribute(BEV);
			return  result;
		} else {
			matrixAdapter.isBtnPermitted(add);
			Long imgId=distributionImagesService.addImg(distributionImages);
			if(imgId!=null){
				return new AjaxResult(AjaxResult.STATUS_OK,imgId.toString(),null,null);
			}
			return new AjaxResult(AjaxResult.STATUS_ERR,null,null,null);
		}
	}
	
   	/**
	 * 进入修改界面
	 */   
	@SaveRequestToken
   	@RequestMapping(value = "/editForm")
	public String editForm(Long id,Long type) {
		/*matrixAdapter.isBtnPermitted(edit);*/
		if (id != null) {
			DistributionProduct distributionProduct = distributionProductService.findById(id);
			WebUtil.getRequest().setAttribute("obj", distributionProduct);
			DistributionImages distributionImages=new DistributionImages();
			distributionImages.setProId(id);
			distributionImages.setImgType(type);
			PaginationVO pageVo=new PaginationVO();
			pageVo.setLimit(0);
			pageVo.setSort("img_sequence");
			pageVo.setOrder("asc");
			List<DistributionImages> list= distributionImagesService.findInPage(distributionImages, null);
			WebUtil.getRequest().setAttribute("list", list);
			WebUtil.getRequest().setAttribute("type", type);
		}
		return "admin/distribution/DistributionImages-form";
	}
   	
   	
   	/**
	 * 删除
	 */  
 	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		matrixAdapter.isBtnPermitted(del);
		return remove(distributionImagesService, keys);
	}
 	/**
	 * 根据List集合更新
	 */  
 	@RequestMapping(value = "/modify")
	public @ResponseBody AjaxResult modify(DistributionImages distributionImages) {
 		int i=distributionImagesService.modifyByList(distributionImages);
 		if(i>0){
 			return new AjaxResult(AjaxResult.STATUS_OK,"产品图集修改成功");
 		}
 		return new AjaxResult(AjaxResult.STATUS_ERR,"产品图集修改失败");
	}
	/**
	 * 删除文件
	 */  
 	@RequestMapping(value = "/delFile")
	public @ResponseBody AjaxResult delFile(DistributionImages distributionImages) {
 		int i=distributionImagesService.removeById(distributionImages.getImgId());
 		if(i>0){
 			return new AjaxResult(AjaxResult.STATUS_OK,"删除成功",null,null);
 		}
 		return new AjaxResult(AjaxResult.STATUS_ERR,"删除失败",null,null);
	}
}